package com.bilibili.controller;

import com.bilibili.dto.VideoInfoDto;
import com.bilibili.dto.VideoToJedis;
import com.bilibili.entity.BulletChat;
import com.bilibili.entity.Video;
import com.bilibili.entity.VideoFlag;
import com.bilibili.service.VideoService;
import com.bilibili.vo.CommentVo;
import com.bilibili.vo.VideoUserMsgVo;
import com.bilibili.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 获取视频的 controller接口
 */
@RestController
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 用于主页面获取随机视频
     *
     * @return
     */
//    @RequestMapping(value = "videos", method = RequestMethod.GET)
//    public List<Video> getrandomvideos() {
//        return videoService.getRandomVideo();
//    }

    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"videoid","userid", "fetch_type=addVideoPlayback"})
    public Boolean addvideoPlayback(@RequestParam(name = "videoid") Integer videoid,
                                    @RequestParam(name = "userid") Integer userId) {
        System.out.println(videoid);
        return videoService.addvideoPlayback(videoid,userId);
    }

    /**
     * 用于id获取指定视频
     *
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"userid", "id"})
    public Video getVideoById(@RequestParam(name = "userid") Integer userId, @RequestParam(name = "id") Integer id) {
        return videoService.getVideoById(id, userId);
    }

    /**
     * 通过视频id获取对应的点击量等
     *
     * @return
     */
    @RequestMapping(value = "videos/{vid}", method = RequestMethod.GET, params = {"fetch_type=getvnumber"})
    public VideoToJedis getVideoToJedisByid(@PathVariable("vid") Integer vid) {
        return videoService.getVideoToJedisByid(vid);
    }

    /**
     * 通过视频id获取弹幕
     *
     * @return
     */
    @RequestMapping(value = "videos/{vid}", method = RequestMethod.GET, params = {"fetch_type=getbullet"})
    public List<BulletChat> getBulletChat(@PathVariable("vid") Integer vid) {
        return videoService.getBulletChatByid(vid);
    }

    /**
     * 添加弹幕
     *
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.POST)
    public boolean addBulletChat(@RequestBody BulletChat bulletChat) {
        System.out.println(bulletChat);
        return videoService.addBulletChat(bulletChat);
    }

    @RequestMapping(value = "videosusers", method = RequestMethod.POST)
    public Integer sendComment(@RequestBody CommentVo commentVo) {
        System.out.println(commentVo);
        return videoService.sendComment(commentVo);
    }

    /**
     * 获取所有视频的信息
     *
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"fetch_type=getAllVideo"})
    public List<VideoVo> getAllVideo() {
        return videoService.getAllVideo();
    }

    /**
     * 添加点赞数
     *
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"changeNumber", "id", "userid"})
    public Boolean changeThumbsUp(@RequestParam(name = "changeNumber") Integer changeNumber,
                                  @RequestParam(name = "id") Integer id,
                                  @RequestParam(name = "userid") Integer userId) {
        return videoService.changeThumbsUp(id, changeNumber, userId);
    }

    /**
     * 添加投币数
     *
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"changecoin", "id", "userid"})
    public Boolean changecoin(@RequestParam(name = "changecoin") Integer changecoin,
                              @RequestParam(name = "id") Integer id,
                              @RequestParam(name = "userid") Integer userId) {
        return videoService.changecoin(id, changecoin, userId);
    }

    /**
     * 添加收藏
     *
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"changecollection", "id", "userid"})
    public Boolean changecollection(@RequestParam(name = "changecollection") Integer changecollection,
                                    @RequestParam(name = "id") Integer id,
                                    @RequestParam(name = "userid") Integer userId) {
        System.out.println(changecollection);
        return videoService.changecollection(id, changecollection, userId);
    }

    /**
     * 获取对应id视频是否要电亮旗帜
     *
     * @param videoid
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"userid", "videoid"})
    public VideoFlag getVideoFlag(@RequestParam(name = "userid") Integer userid,
                                  @RequestParam(name = "videoid") Integer videoid) {
        return videoService.getVideoFlag(videoid, userid);
    }

    /**
     * 获取对应id视频是否要电亮旗帜
     *
     * @param videoid
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"userid", "videoid", "fetch_type=getAllComment"})
    public List<CommentVo> getAllComment(@RequestParam(name = "userid") Integer userId,
                                         @RequestParam(name = "videoid") Integer videoid) {
        return videoService.getAllComment(videoid, userId);
    }

    /**
     * 为视频获取当前用户所需信息的方法
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"userid", "fetch_type=getusermsg"})
    public VideoUserMsgVo getUserMsgById(@RequestParam(name = "userid") Integer userId) {
        return videoService.getUserMsgById(userId);
    }

    /**
     * 投稿所需要的方法
     *
     * @param videoimgurl
     * @param videourl
     * @param userId
     * @param videotime
     * @param videoname
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"videoimgurl", "videourl", "userid", "videotime", "videoname"})
    public Boolean addvideo(@RequestParam(name = "videoimgurl") String videoimgurl,
                            @RequestParam(name = "videourl") String videourl,
                            @RequestParam(name = "userid") Integer userId,
                            @RequestParam(name = "videotime") Double videotime,
                            @RequestParam(name = "videoname") String videoname) {
        return videoService.addvideo(videoimgurl, videourl, userId, videotime, videoname);
    }

    /**
     * 用于投币后
     * @param userId
     * @param coin
     * @return
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"userid", "coin"})
    public Boolean changeUserCoin(@RequestParam(name = "userid") Integer userId,
                                  @RequestParam(name = "coin") Integer coin) {
        System.out.println(userId);
        System.out.println(coin);
        return videoService.changeUserCoin(userId,coin);
    }
    /**
     * 查询最新视频列表
     *
     * @return 最新视频列表
     * @Author YM
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"fetch_type=newest"})
    public List<VideoInfoDto> getNewestVideos() {
        return videoService.getNewestVideos();
    }

    /**
     * 查询最热视频列表
     *
     * @return 最热商品列表
     * @Author YM
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"fetch_type=hotest"})
    public List<Video> getHotestVideos() {
        return videoService.getHotestVideos();
    }

    /**
     * 查询随机视频列表
     *
     * @return 随机商品列表
     * @Author YM
     */
    @RequestMapping(value = "videos", method = RequestMethod.GET, params = {"fetch_type=random"})
    public List<VideoInfoDto> getRandomVideos() {
        return videoService.getRandomVideos();
    }

}
